import sys
import time
import unittest

import selenium.common.exceptions
from selenium.webdriver.support.select import Select

from ltd.hanamaki.python.unittest.paramter.util.MySQLConnectorPythonUtil import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
import ddt

# ddt数据源 列表  元组
dp1 = get_sql_data('register_ecshop',
                   'user_name', 'user_email', 'pass_word',
                   'c_pass_word', 'user_msn', 'user_qq',
                   'work_tel', 'home_tel', 'user_phone',
                   'user_sel_question', 'user_answer')


@ddt.ddt()
class EcShopRegisterUnitTest(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        # 注册 驱动打开的位置
        service = Service(executable_path="../../dirver_exe/geckodriver.exe")
        # 打开浏览器  driver 代表 浏览器对象
        cls.driver = webdriver.Firefox(service=service)
        cls.driver.implicitly_wait(15)  # 隐式等待

    @ddt.data(*dp1)
    def test_ecShop_register(self, data):
        """测试EcShop中的注册功能的使用"""
        try:
            self.driver.get("http://192.168.0.109/ecshop/")
            self.driver.maximize_window()
            if self.driver.page_source.__contains__('退出'):
                self.driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/ul/li[1]/font/font/a[2]").click()
                self.register_f(data)
                self.assert_f()
            else:
                self.register_f(data)
                self.assert_f()
            # 如何 判断注册是否成功
            # 通过断言 判断 注册是否成功
            info = self.driver.page_source.__contains__('退出')
            self.assertEqual(True, info)

        except selenium.common.exceptions.NoAlertPresentException as e:
            print('异常出现')

    def register_f(self, data):
        self.driver.find_element(By.CSS_SELECTOR, "#ECS_MEMBERZONE > a:nth-child(3) > img").click()
        self.driver.find_element(By.ID, "username").send_keys(data[0])
        self.driver.find_element(By.ID, "email").send_keys(data[1])
        self.driver.find_element(By.ID, "password1").send_keys(data[2])
        self.driver.find_element(By.ID, "conform_password").send_keys(data[3])
        self.driver.find_element(By.NAME, "extend_field1").send_keys(data[4])
        self.driver.find_element(By.NAME, "extend_field2").send_keys(data[5])
        self.driver.find_element(By.NAME, "extend_field3").send_keys(data[6])
        self.driver.find_element(By.NAME, "extend_field4").send_keys(data[7])
        self.driver.find_element(By.NAME, "extend_field5").send_keys(data[8])

        sel_question = self.driver.find_element(By.NAME, "sel_question")
        select = Select(sel_question)
        select.select_by_index(data[9])
        self.driver.find_element(By.NAME, "passwd_answer").send_keys(data[10])
        self.driver.find_element(By.NAME, "Submit").click()

    def assert_f(self):
        # 取出 注册时 提示的 错误信息 来添加断言
        error_info = self.driver.switch_to.alert.text
        print(error_info)
        self.driver.switch_to.alert.accept()

        error_info_tuple = error_info.split('- ')
        # 列表 存在多个错 产生的 问题
        error_info_list = []

        for item in error_info_tuple:
            if item.__contains__('密码'):
                error_info_list.append(item)
            elif item.__contains__('MSN'):
                error_info_list.append(item)
            elif item.__contains__('QQ'):
                error_info_list.append(item)
            elif item.__contains__('办公电话'):
                error_info_list.append(item)

        for i in error_info_list:
            self.assertEqual('两次输入密码不一致\n', i, '预期和实际不一致')

    @classmethod
    def teardown_method(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
